<?php
$username = $_POST['username'];
$password = $_POST['password'];
$nickname = $_POST['nickname'];
// 验证用户名
$sql = "SELECT * FROM `login` WHERE `username`='$username'";
// 连接数据库
$link = mysqli_connect('localhost','root','root','bk2109');
$res = mysqli_query($link,$sql);
// 解析
$data = mysqli_fetch_all($res,MYSQLI_ASSOC);
if(count($data)){
      $arr = [
            "message" => '注册失败，用户名已占用',
            "code" => 2
      ];
      echo json_encode($arr);
      exit;
}
// value 插入到数据库
$sql2 = "INSERT INTO `login`(`username`,`password`,`nickname`)VALUES('$username','$password','$nickname')";
$res2 = mysqli_query($link,$sql2);
if($res2){
      $arr = [
            'message' => "注册成功",
            'code' => 1
      ];
}else{
      $arr = [
            "message" => '注册失败',
            "code" => 0
      ];
}
echo json_encode($arr);
?>
